Skip to content

feat: add rename with substitute command#3338

Open
Uanela wants to merge 2 commits into
masterfrom
feat/rename-with-substitute
Open

feat: add rename with substitute command#3338
Uanela wants to merge 2 commits into
masterfrom
feat/rename-with-substitute

Conversation

@Uanela

@Uanela Uanela commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Motivation #3328

I found this feature on Oil and at least for me this was/is the main reason I use Oil, the first one was visual operations like copy/delete/cut and so on, but this one was missing, basically allowing :%s/old/new to change nodes names renaming and writing to disk.

Changes

1 - Added M.bulk_rename into rename-file.lua
2 - Added opts into M.rename to prevent notifications as it is used on a loop
3 - Added opts to get_visual_nodes to correctly allow using '<,'> when needed
4 - (Not quite sure where to put yet or organize), Added an autocmd for watching ex commands

Features

1 - :%s/old/new will select or visible nodes and a simple prompt to rename (substitute) if yes they are written to disk
2 - :'<,'>s/old/new works the same way but on the visual selected part

Concerns

1 - Where and how should the new ex commands autocmd be devided?
2 - Should the autocmd of ex commands stay? or instead create at dedicated something like NvimTreeSubs or whatever to put the tree into substitution mode? - first approach is more natural and expected

I am quite sure that there is a lot of work regarding code organization here, will just wait for your suggestions.

@Uanela Uanela requested review from alex-courtis and v3ceban June 16, 2026 22:03

@v3ceban v3ceban left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered adding something like this in my initial implementation of bulk ops, but ended up not to due to complexities of renaming directories, nested structures, and converting files <-> dirs. If this only targets files I imagine it should be good.

It's missing the mappings to actually try it out, but the logic looks good to me. There are no regressions with existing bulk ops.

I'll leave organization questions for the core maintener to decide on.

Comment thread lua/nvim-tree/utils.lua
Comment on lines +539 to +540
local start_line = vim.fn.line(opts.use_native == true and "'<" or "v")
local end_line = vim.fn.line(opts.use_native == true and "'>" or ".")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

@alex-courtis alex-courtis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking very promising.

I'm not quite sure how to test this. Do I add something like this to API impl? Not sure what the other arguments should be...

api.fs.rename_bulk                           = _v(function(n) require("nvim-tree.actions.fs.rename-file").bulk_rename(n) end)

Please:

Comment thread lua/nvim-tree/autocmd.lua
})
end

vim.api.nvim_create_autocmd("CmdlineLeave", {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, I apologise for the state of autocommands. Having them all in one place is not desirable, and was done purely for startup performance.

Fortunately, this does not have to be a global - it can be placed in it's correct place - rename-file.lua

It can be created when needed i.e. user invokes bulk_rename and removed once the operation is complete. We could probably automatically clean it up after it is done via the {once} option of :help nvim_create_autocmd(). See help.lua for an example.

Comment thread lua/nvim-tree/autocmd.lua
Comment on lines +160 to +162
local core = require("nvim-tree.core")
local explorer = core.get_explorer()
local utils = require("nvim-tree.utils")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename-file.lua already has these, so we should be able to remove these lines 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants